package com.mall.server.controller.ums;

import com.mall.model.domain.UmsAdmin;
import com.mall.model.domain.UmsRole;
import com.mall.model.vo.AdminInfoVo;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.interceptor.UserHolder;
import com.mall.server.service.ums.UmsAdminService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : xyiaao
 * @createDate : 2022/3/30 12:09
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;

    /**
     * 登录返回token
     *
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParam loginParam) {

        Map map = umsAdminService.login(loginParam.getUsername(), loginParam.getPassword());
        return CommonResult.success(map);
    }


    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public CommonResult info() {

        AdminInfoVo adminInfoVo = umsAdminService.info();
        return CommonResult.success(adminInfoVo);
    }


    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id") Long id) {

        umsAdminService.delete(id);
        return CommonResult.success();
    }


    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(String keyword, Integer pageNum, Integer pageSize) {

        CommonPage<UmsAdmin> commonPage = umsAdminService.list(keyword, pageNum, pageSize);
        return CommonResult.success(commonPage);
    }


    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public CommonResult logout() {

        return CommonResult.success();
    }


    /**
     * 刷新token
     *
     * @return
     */
    @GetMapping("/refreshToken")
    public CommonResult refreshToken() {

        Map tokenMap = umsAdminService.getTokenMap(UserHolder.getId());
        return CommonResult.success(tokenMap);
    }


    /**
     * 注册用户
     *
     * @param umsAdmin
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody UmsAdmin umsAdmin) {

        umsAdmin = umsAdminService.register(umsAdmin);
        return CommonResult.success(umsAdmin);
    }


    /**
     * 给用户分配角色
     *
     * @param adminId
     * @param roleIds
     * @return
     */
    @PostMapping("/role/update")
    public CommonResult roleUpdate(Integer adminId, @RequestParam List<Integer> roleIds) {

        umsAdminService.roleUpdate(adminId, roleIds);
        return CommonResult.success();
    }


    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public CommonResult roleAdminId(@PathVariable("adminId") Integer adminId) {

        List<UmsRole> umsRole = umsAdminService.roleAdminId(adminId);
        return CommonResult.success(umsRole);
    }


    /**
     * 修改指定用户信息
     *
     * @param id
     * @param umsAdmin
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id") Integer id, @RequestBody UmsAdmin umsAdmin) {

        umsAdminService.update(umsAdmin);
        return CommonResult.success();
    }


    /**
     * 修改指定用户密码
     *
     * @param updateAdminPasswordParam
     * @return
     */
    @PostMapping("/updatePassword")
    public CommonResult updatePassword(@RequestBody UpdateAdminPasswordParam updateAdminPasswordParam) {

        umsAdminService.updatePassword(updateAdminPasswordParam.getUsername(), updateAdminPasswordParam.getNewPassword());
        return CommonResult.success();
    }


    /**
     * 修改账户状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable("id") Integer id, Integer status) {

        umsAdminService.updateStatus(id, status);
        return CommonResult.success();
    }


    @GetMapping("/{id}")
    public CommonResult getUmsAdmin(@PathVariable("id") Integer id) {

        UmsAdmin umsAdmin = umsAdminService.getUmsAdmin(id);
        return CommonResult.success(umsAdmin);
    }

    /**
     * 修改指定用户密码
     */
    @Data
    private static class UpdateAdminPasswordParam {

        private String newPassword;

        private String oldPassword;

        private String username;

    }

    /**
     * 登录参数
     */
    @Data
    private static class LoginParam {

        private String username;

        private String password;

    }

}
